﻿//////////////////////////////////////////////////////////////////
//
// Encryption.cs
//
// Copyright (c) 2012 Dan Pike.
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation files
// (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Security.Cryptography;
using System.Text;

namespace Utilities
{
   // This code is taken from http://stackoverflow.com/questions/202011/encrypt-decrypt-string-in-net
   // contribution from http://stackoverflow.com/users/193852/andrea-spadaccini
   public class Encryption
   {
      private const string key_ = "wlz7rsRb2";

      ////////////////////////////////////////////////////////////////////////
      //
      public static string Encrypt(string ToEncrypt, bool useHashing)
      {
         byte[] keyArray;
         byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(ToEncrypt);
         //System.Configuration.AppSettingsReader settingsReader = new     AppSettingsReader();
         if (useHashing)
         {
            MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
            keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key_));
            hashmd5.Clear();
         }
         else
         {
            keyArray = UTF8Encoding.UTF8.GetBytes(key_);
         }
         TripleDESCryptoServiceProvider tDes = new TripleDESCryptoServiceProvider();
         tDes.Key = keyArray;
         tDes.Mode = CipherMode.ECB;
         tDes.Padding = PaddingMode.PKCS7;
         ICryptoTransform cTransform = tDes.CreateEncryptor();
         byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
         tDes.Clear();
         return Convert.ToBase64String(resultArray, 0, resultArray.Length);
      }

      ////////////////////////////////////////////////////////////////////////
      //
      public static string Decrypt(string cypherString, bool useHashing)
      {
         byte[] keyArray;
         byte[] toDecryptArray = Convert.FromBase64String(cypherString);
         //byte[] toEncryptArray = Convert.FromBase64String(cypherString);
         //System.Configuration.AppSettingsReader settingReader = new     AppSettingsReader();
         if (useHashing)
         {
            MD5CryptoServiceProvider hashmd = new MD5CryptoServiceProvider();
            keyArray = hashmd.ComputeHash(UTF8Encoding.UTF8.GetBytes(key_));
            hashmd.Clear();
         }
         else
         {
            keyArray = UTF8Encoding.UTF8.GetBytes(key_);
         }
         TripleDESCryptoServiceProvider tDes = new TripleDESCryptoServiceProvider();
         tDes.Key = keyArray;
         tDes.Mode = CipherMode.ECB;
         tDes.Padding = PaddingMode.PKCS7;
         ICryptoTransform cTransform = tDes.CreateDecryptor();
         try
         {
            byte[] resultArray = cTransform.TransformFinalBlock(toDecryptArray, 0, toDecryptArray.Length);

            tDes.Clear();
            return UTF8Encoding.UTF8.GetString(resultArray, 0, resultArray.Length);
         }
         catch (Exception ex)
         {
            throw ex;
         }
      }
   }
}
